package com.example.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@Configuration
@EnableOpenApi
public class SwaggerConfig {
    /**
     * 是否开启swagger
     */
    @Value("${swagger.enabled}")
    private boolean enabled ;

    @Value("${swagger.title}")
    String title;
    @Value("${swagger.description}")
    String description;
    @Value("${swagger.version}")
    String version;
    @Value("${swagger.contact.name}")
    String name;
    @Value("${swagger.contact.url}")
    String url;
    @Value("${swagger.contact.email}")
    String email;

    /**
     * 创建API
     */
//    @Bean
//    public Docket UserApi() {
//        return new Docket(DocumentationType.OAS_30)
//                .enable(enabled)
//                .apiInfo(apiInfo())
//                .groupName("User Api")
//                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
////                .paths(PathSelectors.any())
//                .paths(PathSelectors.ant("/user/**"))
//                .build();
//
//    }
//    @Bean
//    public Docket AdminApi() {
//        return new Docket(DocumentationType.OAS_30)
//                .enable(enabled)
//                .apiInfo(apiInfo())
//                .groupName("Admin Api")
//                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
////                .paths(PathSelectors.any())
//                .paths(PathSelectors.ant("/admin/**"))
//                .build();
//    }
    @Bean
    public Docket otherApi() {
        return new Docket(DocumentationType.OAS_30)
                .enable(enabled)
                .apiInfo(apiInfo())
                .groupName("otherApi")
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
//                .paths(PathSelectors.ant("/"))
                .build();

    }
    /**
     * 添加摘要信息
     */
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .contact(new Contact(name, url, email))
                .license("Apache License Version 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .build();
    }

}
